package br.com.atid.util
{
	import br.com.atid.model.TwoPoints;
	import br.com.atid.model.constants.Constants;
	import br.com.atid.view.util.ArrowStyle;
	import br.com.atid.view.util.GraphicsUtil;
	
	import flash.display.Shape;
	import flash.display.Stage;
	import flash.display.StageDisplayState;
	import flash.geom.Point;
	
	import mx.core.UIComponent;
	import mx.managers.CursorManager;
	
	import spark.core.SpriteVisualElement;

	public class Utils
	{
		public static function removeAllChildren(spriteVisual:SpriteVisualElement):void
		{
			while (spriteVisual.numChildren > 0)
			{
				spriteVisual.removeChildAt(0);
			}
		}

		public static function toogleScreen(stage:Stage):void
		{
			if (stage.displayState == StageDisplayState.FULL_SCREEN_INTERACTIVE || stage.displayState == StageDisplayState.FULL_SCREEN)
			{
				stage.displayState=StageDisplayState.NORMAL;
			}
			else
			{
				stage.displayState=StageDisplayState.FULL_SCREEN;
			}
		}

		public static function drawArcArrow(lineContainer:UIComponent, sourcePoint:Point, destinationPoint:Point, firstElement:Boolean=false):void
		{
			lineContainer.graphics.clear();

			firstElement ? lineContainer.graphics.lineStyle(1, Constants._COLOR_FIRST_ELEMENT, Constants._ALPHA_FIRST_ELEMENT - 0.1) : lineContainer.graphics.lineStyle(1, 0x000000);

			firstElement ? lineContainer.graphics.beginFill(Constants._COLOR_FIRST_ELEMENT) : lineContainer.graphics.beginFill(0x000000);

			var style:ArrowStyle=new ArrowStyle();
			style.headLength=10;
			style.headWidth=8;
			style.shaftPosition=1;
			style.shaftThickness=0.5;
			style.edgeControlPosition=0;
			style.edgeControlSize=1;

			GraphicsUtil.drawArrow(lineContainer.graphics, sourcePoint, destinationPoint, style);
		}

		public static function drawSelectShapeArcArrow(lineContainer:Shape, sourcePoint:Point, destinationPoint:Point):void
		{
			lineContainer.graphics.clear();
			lineContainer.graphics.lineStyle(Constants._THICKNESS, Constants._COLOR_SHAPE_SELECTED, Constants._ALPHA_SELECTED);
			lineContainer.graphics.beginFill(Constants._COLOR_SHAPE_SELECTED, Constants._ALPHA_SELECTED);

			var style:ArrowStyle=new ArrowStyle();
			style.headLength=10;
			style.headWidth=8;
			style.shaftPosition=1;
			style.shaftThickness=0.5;
			style.edgeControlPosition=0;
			style.edgeControlSize=1;

			GraphicsUtil.drawArrow(lineContainer.graphics, sourcePoint, destinationPoint, style);
		}

		public static function drawDeadzoneShapeArcArrow(lineContainer:Shape, sourcePoint:Point, destinationPoint:Point, multiplier:Number):void
		{
			lineContainer.graphics.clear();
			lineContainer.graphics.lineStyle(Constants._THICKNESS * multiplier, Constants._COLOR_DEADZONE_SHAPE, Constants._ALPHA_DEADZONE_SHAPE);
			lineContainer.graphics.beginFill(Constants._COLOR_DEADZONE_SHAPE, Constants._ALPHA_DEADZONE_SHAPE);

			var style:ArrowStyle=new ArrowStyle();
			style.headLength=10;
			style.headWidth=8;
			style.shaftPosition=1;
			style.shaftThickness=0.5;
			style.edgeControlPosition=0;
			style.edgeControlSize=1;

			GraphicsUtil.drawArrow(lineContainer.graphics, sourcePoint, destinationPoint);
		}

		public static function nearestPointBetweenOnePointAndManyPoints(searchPoint:Point, proximityPoints:Array):Point
		{
			var distanceBetween:Array=new Array;
			var index:int;

			for each (var point:Object in proximityPoints)
			{
				distanceBetween.push(Point.distance(searchPoint, Point(point)));
			}

			index=indexMinValue(distanceBetween);
			return proximityPoints[index];
		}
		
		public static function nearestPointBetweenManyPointsAndManyPoints(searchPoints:Array, proximityPoints:Array):TwoPoints
		{
			var distanceBetween:Array=new Array;
			//var smallestDistances:Array=new Array;
			var index:int;
			var count:int = 0;
			var result: TwoPoints = new TwoPoints();
			var p1: Point;
			var p2: Point;
			
			for each (var searchPoint:Object in searchPoints)
			{
				for each (var proximityPoint:Object in proximityPoints)
				{
					distanceBetween.push(Point.distance(Point(searchPoint), Point(proximityPoint)));
				}
			}
			index=indexMinValue(distanceBetween);
			
			for (var i:int = 0; i < searchPoints.length; i++) 
			{
				p1 = searchPoints[i];
				for (var j:int = 0; j < proximityPoints.length; j++) 
				{
					p2 = proximityPoints[j];
					if (count == index)
					{
						result.p1 = p1;
						result.p2 = p2;
						return result;
					}
					count ++;
				}
			}
			return result;
		}		
		
		public static function biggestNumber(numbers: Array): Number {
			return Math.max.apply(null, numbers);
		}

		public static function minValue(values:Array):Number
		{
			var mn:Number=values[0];
			for (var i:int=0; i < values.length; i++)
			{
				if (values[i] < mn)
				{
					mn=values[i];
				}
			}
			return mn;
		}

		public static function indexMinValue(values:Array):int
		{
			var mn:Number=values[0];
			var minIndex:int=0;
			for (var i:int=0; i < values.length; i++)
			{
				if (values[i] < mn)
				{
					mn=values[i];
					minIndex=i;
				}
			}
			return minIndex;
		}
	}
}
